package com.hengda.museumonline.utils;

import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hengda.museumonline.R;

import java.util.List;

/**
 * @author：JFWU on 2016/9/30 11:50
 * @E-mail：jeffy12138@126.com
 */
public class TabIconsUtil {
    private Context context;
    private TabLayout tabLayout;
    private String[] titles;
    private int[] tabIcons;
    private int[] tabIconsPressed;

    public TabIconsUtil(Context context, TabLayout tabLayout, String[] titles, int[] tabIcons, int[] tabIconsPressed) {
        this.context = context;
        this.tabLayout = tabLayout;
        this.titles = titles;
        this.tabIcons = tabIcons;
        this.tabIconsPressed = tabIconsPressed;
    }

    public void setTabIcons2() {
        tabLayout.getTabAt(0).setCustomView(getTabView(0)).setTag(0);
        tabLayout.getTabAt(1).setCustomView(getTabView(1)).setTag(1);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                changeTabSelect(tab);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                changeTabNormal(tab);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
    public void setTabIcons3() {
        tabLayout.getTabAt(0).setCustomView(getTabView(0)).setTag(0);
        tabLayout.getTabAt(1).setCustomView(getTabView(1)).setTag(1);
        tabLayout.getTabAt(2).setCustomView(getTabView(2)).setTag(2);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                changeTabSelect(tab);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                changeTabNormal(tab);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    public View getTabView(int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_tab, null);
        TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
        txt_title.setText(titles[position]);
        ImageView img_title = (ImageView) view.findViewById(R.id.img_title);
        img_title.setImageResource(tabIcons[position]);

        if (position == 0) {
            txt_title.setTextColor(Color.WHITE);
            img_title.setImageResource(tabIconsPressed[position]);
        } else {
            txt_title.setTextColor(context.getResources().getColor(R.color.tabText));
            img_title.setImageResource(tabIcons[position]);
        }
        return view;
    }


    private void changeTabSelect(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        ImageView img_title = (ImageView) view.findViewById(R.id.img_title);
        TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
        txt_title.setTextColor(Color.WHITE);
        img_title.setImageResource(tabIconsPressed[(int) tab.getTag()]);
    }

    private void changeTabNormal(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        ImageView img_title = (ImageView) view.findViewById(R.id.img_title);
        TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
        txt_title.setTextColor(context.getResources().getColor(R.color.tabText));
        img_title.setImageResource(tabIcons[(int) tab.getTag()]);
    }
}
